#include <iostream>
#include <deque>
#include <fstream>
#include <limits>
#include <vector>
#include "BiGraphInt.h"
#include <string>

using namespace std;

bool BFS(BiGraph& bg, BiGraph::vertex_iterator start, BiGraph::vertex_iterator end, double& answ, vector<int>& numbers)
{
    //BFS
    --end;
    end->_parent = NULL;
    deque<BiGraph::Vertex*> q;
    q.push_back(bg.add_vertex(start->getName()));
    start->getColour() = BiGraph::C_GRAY;
    while(!q.empty())
    {
        BiGraph::Vertex *head = q.front();
        if (head->getName() == end->getName())
            break;
        q.pop_front();
        head->getColour() = BiGraph::C_BLACK;
        for(BiGraph::edge_iterator_out e_io = head->begin_out(); e_io != head->end_out(); ++e_io)
        {
            if(e_io->get_end_vertex()->getColour() == BiGraph::C_WHITE)
            {
                q.push_back(e_io->get_end_vertex());
                e_io->get_end_vertex()->getColour() = BiGraph::C_GRAY;
                e_io->get_end_vertex()->_parent = head;// the path of stream
            }
        }
    }
    if (end->_parent == NULL)
        return false;
    //other work
    BiGraph::Vertex* start_v = bg.add_vertex(start->getName());
    double min = start->begin_out()->get_weight();
    for (BiGraph::Vertex* vi = bg.add_vertex(end->getName()); vi != start_v; vi = vi->_parent)
        for (BiGraph::edge_iterator_in eio = vi->begin_in(); eio != vi->end_in(); ++eio)
            if (eio->get_begin_vertex()->getName() == vi->_parent->getName() && eio->get_weight() < min)//find the path and
                min = eio->get_weight();                                                           //calculate the power of stream

    for (BiGraph::Vertex* vi = bg.add_vertex(end->getName()); vi != start_v; vi = vi->_parent)
        for (BiGraph::edge_iterator_in eio = vi->begin_in(); eio != vi->end_in(); ++eio) // the edges iteration
            if (eio->get_begin_vertex()->getName() == vi->_parent->getName()){
                if (vi->getName() > 0 && vi->getName() != numeric_limits<int>::max()) 
                    numbers.push_back(vi->getName()); // add number of cubes for output
                if (eio->get_weight() > min) // if there are more capacity
                    eio->get_weight() -= min;
                else
                {
                    bg.delete_edge(eio->get_begin_vertex()->getName(), eio->get_end_vertex()->getName()); //if there are not more capacity
                    break;
                };
            }
    for (BiGraph::vertex_iterator vi = bg.begin(); vi != bg.end(); ++vi) 
        vi->getColour() = BiGraph::C_WHITE;// clear colour for future work
    answ += min; // add to answer
    return true;
};

size_t getMatrix(BiGraph& bg)
{
    int n;
    string str;
    ifstream ifile("in.txt");
    ifile >> str;
    ifile >> n;
    for (int i = 0; i < n; ++i)
    {   
        string tmp;
        bg.add_edge(numeric_limits<int>::min(), i, 1);//add min == first vertex - from the vertex the streams is begun; 
        bg.add_edge(i, numeric_limits<int>::min(), 1);//capacity for each cube == 1; add i == number of the cube; 
        ifile >> tmp;
        for (size_t j = 0; j < str.length(); ++j) {
            for (size_t l = 0; l < tmp.length(); ++l) {
                if (str[j] == tmp[l]) {
                    bg.add_edge(i, -str[j], 1);// add -str[j] == the letters of cube
                    bg.add_edge(-str[j], i, 1);
                    bg.add_edge(-str[j], numeric_limits<int>::max(), 1); // add max == last vertex
                    bg.add_edge(numeric_limits<int>::max(), -str[j], 1);
                }
            }
        }
    }
    ifile.close();
    return str.length();
};

double A_Edmond_Karp(BiGraph& bg, vector<int>& numbers)
{
    double answ = 0;
    while(BFS(bg, bg.begin(), bg.end(), answ, numbers)) {};
    return answ;
}

int main()
{
    BiGraph bg;
    vector<int> numbers;
    int answ = getMatrix(bg);
    if (answ == A_Edmond_Karp(bg, numbers)) {
        cout << "YES" << endl;
        for (size_t i = 0; i < numbers.size(); ++i)
        {
            cout << numbers[i] << " ";
        }
        cout << endl;
    }
    else
        cout << "NO" << endl;
    return 0;
}